import { defineStore } from "pinia";
import http from "../utils/http";
import { message } from 'ant-design-vue';
export const usePage = defineStore("page", {
    state: () => {
        return {
            pageList: [],
            total:0,
            isaddpage:"",
        }
    },
    actions: {
        //获取页面数据
        async getpages(page, pageSize) {
            let res = await http.get("/api/page", { params: { page, pageSize } });
            // console.log(res, "page")
            if (res.status === 200) {
                //处理后端发送过来的数据
                let newdata = res.data.data[0].map((v, i) => {
                    v.key = i
                    v.createAt = new Date(v.createAt).toLocaleString()
                    return v
                })
                this.pageList = newdata
                this.total = res.data.data[1]
            }
        },
        //模糊搜索
        async getsearchpages(obj) {
            let res = await http.get("/api/page", { params: obj })
            if (res.status === 200) {
                //处理后端发送过来的数据
                let newdata = res.data.data[0].map((v, i) => {
                    v.key = i
                    v.createAt = new Date(v.createAt).toLocaleString()
                    return v
                })
                this.pageList = newdata
                this.total = res.data.data[1]
            }
        },
        //改变发布和下线
        async editpublish(obj) {
            let res = await http.patch("/api/page/" + obj.id, { status: obj.status });
            if (res.status == 200) {
                this.getpages(1, 12)
            }
        },
        //删除单个数据
        async removepage(id) {
            let res = await http.delete("/api/page/" + id);
            // console.log(res,"res")
            if (res.status === 200) {
                this.getpages(1, 12)
            }
        },
        //批量删除数据
        pageallremove(arr) {
            arr.map(item => {
                this.removepage(this.pageList[item].id)
            })
        },
        //批量发布
        pageallpublish(arr) {
            arr.map(item => {
                this.editpublish({ id: this.pageList[item].id, status: "publish" })
            })
        },
        //批量下线
        pagealldraft(arr) {
            arr.map(item => {
                this.editpublish({ id: this.pageList[item].id, status: "draft" })
            })
        },
        //添加页面
        async adddpages(obj){
            let res = await http.post("/api/page",obj);
            console.log(res,"res") 
            if(res.status==201){
                this.isaddpage = res.data.data.id
            }
        },
        //更新编辑页面
        async editpages(id,obj){
            let res = await http.patch("/api/page/"+id,obj);
            if(res.status==200){
                message.success("修改成功")
            }
        }
    },
    getters: {

    }
})